﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory.Achievement.Strings

    ''' <summary>
    ''' A class that retrieves achievement strings from the Armory
    ''' </summary>
    ''' <remarks></remarks>
    Public Class AchievementStrings
        Inherits WowArmoryData

        Private assPage As New Schema.page

        ''' <summary>
        ''' The achievement categories currently available
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Categories As Collection(Of CategoryInfo)
            Get
                If assPage Is Nothing Then
                    Return New Collection(Of CategoryInfo)
                End If
                Return (From c In assPage.rootCategories.SelectMany(Function(rc) rc.category)
                        Select New CategoryInfo(c.id, c.name,
                                                (From sc In c.category Select New SubcategoryInfo(sc.name)).ToCollection)).ToCollection
            End Get
        End Property

        ''' <summary>
        ''' The achievements currently available
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>The achievements returned here are not categorized.</remarks>
        Public ReadOnly Property Achievements As Collection(Of Achievement)
            Get
                If assPage Is Nothing Then
                    Return New Collection(Of Achievement)
                End If
                Return (From a In assPage.achievements.SelectMany(Function(a) a.achievement)
                        Select New Achievement(a.desc, a.icon, a.id, a.reward, a.title)).ToCollection
            End Get
        End Property

        ''' <summary>
        ''' The URL that will be used to retrieve the achievement strings from the Armory
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Return "http://www.wowarmory.com/data/achievementStrings.xml?loc=en_us"
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return False
            End Get
        End Property

        Public Sub New()
            If Cache.Cache("LibWowArmory.AchievementStrings") Is Nothing Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(assPage.GetType)

                Using srXML As New StringReader(MyBase.Data)
                    assPage = CType(xsSerializer.Deserialize(srXML), Schema.page)
                End Using

                Cache.Cache.Insert("LibWowArmory.AchievementStrings", assPage, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.NotRemovable, Nothing)
            Else
                assPage = CType(Cache.Cache("LibWowArmory.AchievementStrings"), Schema.page)
            End If
        End Sub

    End Class

End Namespace
